#!/bin/bash
function untabify(){
    cd $1
    find . -name '*.c' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;
    find . -name '*.cpp' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;
    find . -name '*.h' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;
    find . -name '*.hpp' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;
}

function recursive(){
    for element in `ls $1`
    do  
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then 
            echo $dir_or_file
            untabify $dir_or_file
            recursive $dir_or_file
        # else
        #     echo $dir_or_file
        fi  
    done
}


read -p "please enter the dir path:" path #读取目录路径
if [ ! -x "$path" ];    #判断目录是否存在且是否具有执行权限
then
    echo "dir path not exists"
else
    read -p "please enter if you want to recursive?y/n:" recur  #是否递归
fi


if [ $recur = "y" ];
then
    recursive "$path"
else
    untabify "$path"     #非递归修改
fi